package leetcode16;

public class test90 {
    /**
     * 1143. 最长公共子序列
     * @param text1
     * @param text2
     * @return
     */
    public int longestCommonSubsequence(String text1, String text2) {
        char[] s = text1.toCharArray();
        char[] t = text2.toCharArray();
        int n = s.length;
        int m = t.length;
        int[][] dp = new int[2][m + 1];
        for(int i = 0;i < n;i++){
            for(int j = 0;j < m;j++){
                dp[(i + 1) % 2][j + 1] = (s[i] == t[j])
                        ? dp[i % 2][j] + 1 : Math.max(dp[i % 2][j + 1],dp[(i + 1) % 2][j]);
            }
        }
        return dp[n % 2][m];
    }
}
